package wiki.servlets;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import wiki.generic.utilities.AlreadyExistingElementException;
import wiki.setup.Setup;
/**
 * 
 * @author <p><strong>JwTeam</strong></p> 
 * Responsabile della fase di registrazione di nuovi utenti
 */
public class RegistrationServlet extends HttpServlet{

	
	private static final long serialVersionUID = 1L;

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		
		String nome= req.getParameter("nome");
		String cognome=req.getParameter("cognome");
		String email=req.getParameter("email");
		String password=req.getParameter("password");
		String corso=req.getParameter("corso");
		nome=Setup.getSetup().getMediator().getGpService().changeInputText(nome);
		cognome=Setup.getSetup().getMediator().getGpService().changeInputText(cognome);
		email=email.replace("$$||$$", "$$$$$$");
		password=password.replace("$$||$$", "$$$$$$");
		if(!Setup.getSetup().getMediator().getLoginService().getCoursesMap().containsKey(corso)){
			resp.sendRedirect("./html/errorpages/nocorso.html");
			return;
		}
		
			try {
				Setup.getSetup().getMediator().getLoginService().addUser(nome, cognome, password, corso, email);
				resp.sendRedirect("./html/registrazione/notificaRegistrazione.html");
			} catch (AlreadyExistingElementException e) {
				resp.sendRedirect("./html/registrazione/registrazioneFailed.html");
			}
			
	} 
		
		@Override
		protected void doGet(HttpServletRequest req, HttpServletResponse resp)
				throws ServletException, IOException {
			resp.sendRedirect("./html/errorpages/permessi.html");
			
		}
		
		
}
	
